package com.j4dream.property.javabasic.designpattern.filterchain;

import org.apache.commons.compress.utils.Lists;

import java.util.List;

public class FilterChain implements Filter{

    private List<Filter> filters = Lists.newArrayList();

    private int currentPosition = 0;

    public FilterChain addFilter(Filter filter){
        this.filters.add(filter);
        return this;
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {

        if (this.currentPosition == this.filters.size()) {
            return;
        }
        this.currentPosition++;
        Filter nextFilter = this.filters.get(this.currentPosition - 1);
        nextFilter.doFilter(request, response, this);
    }
}
